//
// Created by liangzhenying on 16/2/17.
// Copyright (c) 2016 LiangZhenYing. All rights reserved.
//

#import "YLBannerView.h"
#import "YLImageViews.h"

@interface YLBannerView () <YLImageViewsDelegate>
@property (nonatomic,strong)YLImageViews *imageViews;
@property (nonatomic,strong)UIPageControl *pageControl;
@end

@implementation YLBannerView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) return nil;

    UIImageView *bgView = [[UIImageView alloc]initWithFrame:self.bounds];
    bgView.image = [UIImage imageNamed:@"banner"];
    [self addSubview:bgView];
    
    YLImageViews *imageViews = [[YLImageViews alloc] initWithFrame:self.bounds];
    imageViews.backgroundColor = [UIColor clearColor];
    imageViews.imgDelegate = self;
    self.imageViews = imageViews;
    [self addSubview:imageViews];

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 20, frame.size.width, 20)];
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    pageControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    pageControl.hidesForSinglePage = YES;
    self.pageControl = pageControl;
    [self addSubview:pageControl];

    return self;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.pageControl.numberOfPages = dataArray.count;
    self.imageViews.urls = dataArray;
    [self.imageViews update];
}

- (void)startAnimation {
    [self.imageViews update];
}

- (void)stopAnimation {
    [self.imageViews releaseTimer];
}

#pragma mark - YLImageViewsDelegate
-(void)imageViews:(YLImageViews *)view showPage:(int)page{
    self.pageControl.currentPage = page;
}

-(void)imageViews:(YLImageViews *)view didClick:(id)object{
    if (self.delegate && [self.delegate respondsToSelector:@selector(bannerView:didClickPage:)]) {
        NSInteger index = [self.dataArray indexOfObject:object];
        [self.delegate bannerView:self didClickPage:index];
    }
}
@end
